#import "LikeRequest.h"
#import "RequestConfig.h"

@interface LikeRequest()
@property (nonatomic,assign)NSString *host;
@property (nonatomic,retain)AFHTTPSessionManager *manager;
@end
LikeRequest *default_like_request;
// content
@implementation LikeRequest
- (void)initReq:(RequestConfig*)config{
    _host = [config host];
    _manager = [config manager];
}

+ (id)defaultLikeRequest{
    if (default_like_request == nil) {
        default_like_request = [[LikeRequest alloc]init];
        [default_like_request initReq:[RequestConfig defaultConfig]];
    }
    return default_like_request;
}
-(void)getLikeList:(void (^)(NSArray<NSString *> *))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/like",_host];
    [_manager GET:url parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            NSArray<NSString*> *list = responseObject[@"Data"];
            success(list);
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)likeContentByID:(NSString *)ContentID success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/like/%@",_host,ContentID];
    NSDictionary *dict = @{@"isContent":@YES,@"isComment":@NO,@"isReply":@NO};
    [_manager POST:url parameters:dict headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)unlikeContentByID:(NSString *)ContentID success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/like/%@",_host,ContentID];
    NSDictionary *dict = @{@"isContent":@YES,@"isComment":@NO,@"isReply":@NO};
    [_manager PATCH:url parameters:dict headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)likeCommentByID:(NSString *)CommentID success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/like/%@",_host,CommentID];
    NSDictionary *dict = @{@"isContent":@NO,@"isComment":@YES,@"isReply":@NO};
    [_manager POST:url parameters:dict headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)unlikeCommentByID:(NSString *)CommentID success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/like/%@",_host,CommentID];
    NSDictionary *dict = @{@"isContent":@NO,@"isComment":@YES,@"isReply":@NO};
    [_manager PATCH:url parameters:dict headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)likeReplyByID:(NSString *)ReplyID success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/like/%@",_host,ReplyID];
    NSDictionary *dict = @{@"isContent":@NO,@"isComment":@NO,@"isReply":@YES};
    [_manager POST:url parameters:dict headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)unlikeReplyByID:(NSString *)ReplyID success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/like/%@",_host,ReplyID];
    NSDictionary *dict = @{@"isContent":@NO,@"isComment":@NO,@"isReply":@YES};
    [_manager PATCH:url parameters:dict headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}

@end
